# -*- coding:utf-8 -*-
# @FileName  :params_operator.py
# @Time      :2020/10/25 14:59
# @Author    :zhoujirong
import os
import xlrd
from conf.settings import CASE_DATA_PATH
from common.utils import get_mysql

class Param:
    @classmethod
    def from_csv(cls,filename):
        '''从csv/txt文件中读取文件内容成二维数组'''
        abs_path = os.path.join(CASE_DATA_PATH,filename)
        data = []
        with open(abs_path,encoding='utf-8') as fr:
            for line in fr:
                line = line.strip()
                if line:
                    data.append(line.split(','))
        return data

    @classmethod
    def from_excel(cls,name):
        '''从csv/txt文件中读取文件内容成二维数组'''
        data = []
        abs_path = os.path.join(CASE_DATA_PATH,name)
        book = xlrd.open_workbook(abs_path)
        sheet = book.sheet_by_index(0)
        for i in range(1,sheet.nrows):
            data.append(sheet.row_values(i))
        return data

    @classmethod
    def from_yaml(cls):
        pass


    @classmethod
    def from_mysql(cls, sql,db=None,node = 'mysql'):
        m = get_mysql(node,db=db,cursor_type=None)
        return m.fetchall(sql)


    @classmethod
    def from_redis(cls):
        pass
    def from_mongodb(self):
        pass

if __name__ == "__main__":
    # print(Param.from_csv(r'E:\study\scripts\python\TMZ0\day12\cqs\biz\data\users'))
    print(Param.from_excel(r'E:\study\scripts\python\TMZ0\day12\rainbow\biz\data\users.xls'))